import errno
import socket
import threading
import time
import datetime

EOL1 = b'\n\n'
EOL2 = b'\n\r\n'
body = """Hello world! <h1> from th5fire 《Django 企业开发实战》</h1> - from {thread_name}"""
response_params = [
    'HTTP/1.0 200 OK',
    datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    'Content-Type: text/plain; charset=utf-8',
    'Content-Length: (length)\r\n',
    body
]

response = '\r\n'.join(response_params)


def handle_connection(conn, addr):
    #print(conn, addr)
    #time.sleep(60)
    request = b""
    while EOL1 not in request and EOL2 not in request:
        request += conn.recv(1024)

    print(request)
    current_thread = threading.currentThread()
    current_length = len(body.format(thread_name=current_thread.name).encode())
    print(current_thread.name)
    conn.send(response.format(thread_name = current_thread.name, length = current_length).encode())
    conn.close()
def main():
    pass